home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / video / thrust-s.53 / thrust-s / thrust / src / init.c < prev    next >
C/C++ Source or Header  |  1995-11-03  |  5KB  |  266 lines

  1.  
  2. /* Written by Peter Ekberg, peda@lysator.liu.se */
  3.  
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <vga.h>
  7. #include <vgagl.h>
  8. #include <vgakeyboard.h>
  9. #include <malloc.h>
  10. #include "thrust.h"
  11.  
  12. void
  13. turnship()
  14. {
  15.   word i,j,k;
  16.  
  17.   for(k=0; k<4; k++)
  18.     for(i=0; i<16; i++)
  19.       for(j=0; j<16; j++)
  20.     *(ship+(5+k)*256 +     i*16+j)=
  21.       *(ship+(3-k)*256 +(15-j)*16+15-i);
  22.   for(i=0; i<4; i++) {
  23.     memcpy(shipstorage,ship+(i<<8),256);
  24.     memcpy(ship+(i<<8),ship+((8-i)<<8),256);
  25.     memcpy(ship+((8-i)<<8),shipstorage,256);
  26.   }
  27.   for(k=0; k<8; k++)
  28.     for(i=0; i<16; i++)
  29.       for(j=0; j<16; j++)
  30.     *(ship+(9+k)*256 +     i*16+j)=
  31.       *(ship+(7-k)*256 +     i*16+15-j);
  32.   for(k=0; k<15; k++)
  33.     for(i=0; i<16; i++)
  34.       for(j=0; j<16; j++)
  35.     *(ship+(17+k)*256+     i*16+j)=
  36.       *(ship+(15-k)*256+(15-i)*16+j);
  37. }
  38.  
  39. void
  40. makeshieldedship()
  41. {
  42.   word i,j,k;
  43.  
  44.   memcpy(shieldship, ship, 8192);
  45.   for(i=0; i<32; i++)
  46.     for(j=0; j<16; j++)
  47.       for(k=0; k<16; k++)
  48.     if(*(bin_shield+(j<<4)+k))
  49.       *(shieldship+(i<<8)+(j<<4)+k)=*(bin_shield+(j<<4)+k);
  50. }
  51.  
  52. void
  53. makefuelmap(fuelmap)
  54.      dword *fuelmap;
  55. {
  56.   int i;
  57.  
  58.   for(i=0; i<32; i++)
  59.     {
  60.       *(fuelmap+i+32)=255UL<<((i>6)+(i>16)+(i>26))*8;
  61.       *(fuelmap+i)=255UL<<((i<27)+(i<17)+(i<7))*8;
  62.     }
  63. }
  64.  
  65. int
  66. initmem()
  67. {
  68.   int i;
  69.  
  70.   bild=(byte *)malloc((long)PBILDX*PBILDY*2+16);
  71.   bana=(byte *)malloc(maxlenx*maxleny);
  72.   ship=(byte *)malloc(8192);
  73.   shieldship=(byte *)malloc(8192);
  74.   shipstorage=(byte *)malloc(256);
  75.   bulletmap=(byte *)malloc(256);
  76.   bulletstorage=(byte *)malloc(maxbullets*16);
  77.   fragmentstorage=(byte *)malloc(maxfragments*4);
  78.   fuelmap=(byte *)malloc(256);
  79.   fuelstorage=(byte *)malloc(256);
  80.   loadmap=(byte *)malloc(64);
  81.   loadstorage=(byte *)malloc(64);
  82.   wirestorage=(byte *)malloc(64);
  83.  
  84.   printf("Allocating memory...");
  85.  
  86.   if(!bana || !bild || !ship || !shieldship || !shipstorage ||
  87.      !bulletmap || !bulletstorage || !fuelmap || !fuelstorage ||
  88.      !loadmap || !loadstorage || !fragmentstorage) {
  89.     printf("failed!.\n");
  90.     return(0);
  91.   }
  92.   printf("done.\n");
  93.  
  94.   blocks=bin_blocks;
  95.   memcpy(ship, bin_ship, 256*5);
  96.   for(i=0; i<16; i++)
  97.     memcpy(bulletmap+((20-i)&15)*16, bin_bullets+i*16, 16);
  98.  
  99.   printf("Turning the ship...");
  100.   turnship();
  101.   printf("done.\n");
  102.   printf("Building graphics...");
  103.   makefuelmap((dword *)fuelmap);
  104.   memcpy(loadmap, blocks+64*253, 64);
  105.   makeshieldedship();
  106.   printf("done.\n");
  107.  
  108.   return(1);
  109. }
  110.  
  111. void
  112. inithardware()
  113. {
  114.   vga_init();
  115.   if(!vga_hasmode(VGAMODE)) {
  116.     printf("Graphics mode is not available.\n");
  117.     exit(-1);
  118.   }
  119.   vga_setmode(VGAMODE);
  120.   gl_setcontextvga(VGAMODE);
  121.   
  122.   palette=(Palette *)bin_colors;
  123.   fadepalette(0,255,palette,1,0);
  124.  
  125.   if(keyboard_init()) {
  126.     printf("Could not initialize the keyboard.\n");
  127.     exit(-1);
  128.   }
  129.   printf("Keyboard initialized.\n");
  130. }
  131.  
  132. void
  133. initscreen(void)
  134. {
  135.   int i,j;
  136.  
  137.   for(j=pblocky; j<BBILDY+pblocky; j++)
  138.     for(i=pblockx; i<BBILDX+pblockx; i++)
  139.       writeblock(i%lenx, j, *(bana+i%lenx+j*lenx));
  140. }
  141.  
  142. void
  143. initgame(int round, int reset, int xblock, int yblock)
  144. {
  145.   int i;
  146.  
  147.   crash=0;
  148.   shoot=0;
  149. #ifdef DEBUG
  150.   repetetive=1;
  151. #else
  152.   repetetive=0;
  153. #endif
  154.   refueling=0;
  155.   speedx=0;
  156.   speedy=0;
  157.   absspeed=0L;
  158.   oldabs=0L;
  159.   vx=0;
  160.   vy=0;
  161.   if(round&1) {
  162.     kdir=72;
  163.     dir=24;
  164.     gravity=-20;
  165.     alpha=768;
  166.     deltaalpha=0;
  167.   }
  168.   else {
  169.     kdir=24;
  170.     dir=8;
  171.     gravity=20;
  172.     alpha=256;
  173.     deltaalpha=0;
  174.   }
  175.   if(round&2) {
  176.     palette->color[65].red  =0;
  177.     palette->color[65].green=0;
  178.     palette->color[65].blue =0;
  179.   }
  180.   else {
  181.     palette->color[65].red  =colorr;
  182.     palette->color[65].green=colorg;
  183.     palette->color[65].blue =colorb;
  184.   }
  185.   if(reset) {
  186.     loaded=0;
  187.     loadcontact=0;
  188.     loadpoint=0;
  189.     loadpointshift=0;
  190.     shipdx=0;
  191.     shipdy=0;
  192.   }
  193.   else {
  194.     loadcontact=0;
  195.     if(loaded) {
  196.       loadpoint=126;
  197.       loadpointshift=0;
  198.       shipdx=(sinus2[(alpha+256)&1023]*loadpoint)/1512;
  199.       shipdy=(-sinus2[alpha]*loadpoint)/1512;
  200.     }
  201.     else {
  202.       loadpoint=0;
  203.       loadpointshift=0;
  204.       *(bana+lenx*loadby+loadbx)=253;
  205.       shipdx=0;
  206.       shipdy=0;
  207.     }
  208.   }
  209.   
  210.   pblockx=xblock;
  211.   pblocky=yblock+4*(round&1);
  212.   if(loaded) {
  213.     if(round&1)
  214.       pblocky-=2;
  215.     else
  216.       pblocky+=2;
  217.   }
  218.  
  219.   pixx=pblockx<<3;
  220.   pixy=pblocky<<3;
  221.   x=pixx<<3;
  222.   y=pixy<<3;
  223.   bildx=(pixx+PBILDX-4)%PBILDX+4;
  224.   bildy=pixy%PBILDY;
  225.   bblockx=bildx>>3;
  226.   bblocky=bildy>>3;
  227.  
  228.   countdown=0;
  229.  
  230.   for(i=0; i<maxbullets; i++)
  231.     bullets[i].life=0;
  232.  
  233.   chcolor=20;
  234.   chpaper=0;
  235.   chflag=0;
  236. }
  237.  
  238. void
  239. restorehardware()
  240. {
  241.   printf("Releasing keyboard...");
  242.   keyboard_close();
  243.   printf("done.\n");
  244.   printf("Restoring textmode...");
  245.   vga_setmode(TEXT);
  246.   printf("done.\n");
  247. }
  248.  
  249. void
  250. restoremem()
  251. {
  252.   printf("Freeing allocated memory...");
  253.   free(bild);
  254.   free(bana);
  255.   free(bulletmap);
  256.   free(ship);
  257.   free(shieldship);
  258.   free(shipstorage);
  259.   free(bulletstorage);
  260.   free(fuelmap);
  261.   free(fuelstorage);
  262.   free(loadmap);
  263.   free(loadstorage);
  264.   printf("done.\n");
  265. }
  266.